package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.enums.RedisKeyEnum;
import zhihuilan.common.utils.RedisCache;
import zhihuilan.entity.MyCategoryUnit;
import zhihuilan.mapper.MyCategoryUnitMapper;
import zhihuilan.params.query.MyCategoryUnitQueryParam;
import zhihuilan.service.MyCategoryUnitService;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyCategoryUnitServiceImpl extends ServiceImpl<MyCategoryUnitMapper, MyCategoryUnit> implements MyCategoryUnitService {

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<MyCategoryUnit> selectList(MyCategoryUnitQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        QueryWrapper<MyCategoryUnit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("isdelete", 0);

        if (!ObjectUtils.isEmpty(queryParam.getName())) {
            queryWrapper.eq("name", queryParam.getName());
        }
        List<MyCategoryUnit> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public MyCategoryUnit selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }

        return this.baseMapper.selectById(id);
    }

    @Transactional
    @Override
    public MyCategoryUnit addEntity(MyCategoryUnit entity) throws RuntimeException {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        entity.setAddtime(new Date());
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public MyCategoryUnit updateEntity(MyCategoryUnit entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId()) || entity.getId() == 0) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return null;
    }

    @Override
    public Boolean deletetById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        int delete = this.baseMapper.deleteById(id);
        return delete > 0;
    }

    @Override
    public String getCategoryUnitName(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return "";
        }
        String key = RedisKeyEnum.REDIS_KEY_UNIT.getKey() + id;
        String result = redisCache.getCacheObject(key);
        if (ObjectUtils.isEmpty(result)) {
            MyCategoryUnit myCategoryUnit = this.baseMapper.selectById(id);
            if (ObjectUtils.isEmpty(myCategoryUnit)) {
                return "";
            }
            if (ObjectUtils.isEmpty(myCategoryUnit.getName())) {
                return "";
            }
            result = myCategoryUnit.getName();
            redisCache.setCacheObject(key, result, 2, TimeUnit.HOURS);
        }
        return result;
    }

}
